//
//  DanoneStoolDao.m
//  DanoneEA
//
//  Created by 3ti_Jery on 16/10/19.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import "DNStoolDao.h"
#import "DNUserManager.h"

@implementation DNStoolDao

+ (NSString *) getCreateSQL
{
    return [NSString
            stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ text primary key ,%@ text,%@ text,%@ integer,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ text,%@ integer,%@ text);",
            Stool_TABLE,
            DairyBase_AppId,
            DairyBase_ServerId,
            DairyBase_InfantId,
            DairyBase_Status,
            DairyBase_ModifyReason,
            DairyBase_DeleteReason,
            DairyBase_CreateTime,
            DairyBase_ModifyTime,
            DairyBase_DeleteTime,
            DairyBase_Ddat,
            Stool_StoolYn,
            Stool_Image,
            Stool_StoolCon,
            Stool_amount
            ];
    
}

+ (NSString *) getInsertSQL
{
    return [NSString stringWithFormat:@"REPLACE INTO %@(%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
            Stool_TABLE,
            DairyBase_AppId,
            DairyBase_ServerId,
            DairyBase_InfantId,
            DairyBase_Status,
            DairyBase_ModifyReason,
            DairyBase_DeleteReason,
            DairyBase_CreateTime,
            DairyBase_ModifyTime,
            DairyBase_DeleteTime,
            DairyBase_Ddat,
            Stool_StoolYn,
            Stool_Image,
            Stool_StoolCon,
            Stool_amount
            ];
    
}

#pragma mark - add

- (BOOL)saveStoolModel:(DNStoolModel *) object
{
    
    DNDataBase *dataBase = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    
    [dataBase.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }else{
            [db setKey:DB_SECRETKEY];
            ret =[db executeUpdate:[DNStoolDao getInsertSQL],
                  object.appId,
                  object.serverId,
                  object.infantId,
                  object.status,
                  object.modifyReason,
                  object.deleteReason,
                  object.createTime,
                  object.modifyTime,
                  object.deleteTime,
                  object.ddat,
                  object.stoolYn,
                  object.image,
                  object.stoolCon,
                  object.amount
                  ];
            
            if (!ret) {
                ret = NO;
                [db rollback];;
                DDLogError(@"Update error AppId:%@",
                           object.appId);
                [db close];
                return;
            }
        }
        [db close];
    }];
    
    return ret;
}

- (BOOL)saveStoolModelWithArray:(NSArray *)objectList
{
    
    __block BOOL ret = YES;
    DNDataBase *dataBase = [DNDataBase shareInstance];
    // 如果要支持事务
    [dataBase.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        [db setKey:DB_SECRETKEY];
            
        for (DNStoolModel *object in objectList) {
            
            ret =[db executeUpdate:[DNStoolDao getInsertSQL],
                  object.appId,
                  object.serverId,
                  object.infantId,
                  object.status,
                  object.modifyReason,
                  object.deleteReason,
                  object.createTime,
                  object.modifyTime,
                  object.deleteTime,
                  object.ddat,
                  object.stoolYn,
                  object.image,
                  object.stoolCon,
                  object.amount
                  ];
            
            if (!ret) {
                ret = NO;
                *rollback = YES;
                DDLogError(@"Update error AppId:%@",
                           object.appId);
                return;
            }
        }
    }];
    return ret;

}


#pragma mark - delete

- (BOOL)deleteStoolModelByAppId:(NSString *)appId
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString * deleteSQL = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = %@;",Stool_TABLE,DairyBase_AppId,appId];
        ret = [db executeUpdate:deleteSQL];
        [db close];
        
    }];
    return ret;

}

#pragma mark - update

- (BOOL)updateStoolModel:(DNStoolModel *)object
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString * updateString = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' ,%@ = '%@',%@ = %@,%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = '%@',%@ = %@,%@ = '%@',%@ = '%@',%@ = '%@' WHERE %@ = '%@'",
                                   Stool_TABLE,
                                   DairyBase_ServerId,object.serverId,
                                   DairyBase_InfantId,object.infantId,
                                   DairyBase_Status,object.status,
                                   DairyBase_ModifyReason,object.modifyReason,
                                   DairyBase_DeleteReason,object.deleteReason,
                                   DairyBase_CreateTime,object.createTime,
                                   DairyBase_ModifyTime,object.modifyTime,
                                   DairyBase_DeleteTime,object.deleteTime,
                                   DairyBase_Ddat,object.ddat,
                                   Stool_StoolYn,object.stoolYn,
                                   Stool_Image,object.image,
                                   Stool_StoolCon,object.stoolCon,
                                   Stool_amount,object.amount,
                                   DairyBase_AppId,object.appId
                                   ];
        ret = [db executeUpdate:updateString];
        [db close];
    }];
    return ret;

}

- (BOOL)updateStoolModelByAppId:(NSString *)appId
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block BOOL ret = NO;
    
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        DNStoolModel *object = [self getObjectModelByAppId:appId];
        
        NSString * updateString = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@'",
                                   Stool_TABLE,
                                   DairyBase_Status,object.status,
                                   DairyBase_AppId,object.appId
                                   ];
        ret = [db executeUpdate:updateString];
        [db close];
    }];
    return ret;

}

#pragma mark - query

- (DNStoolModel *)getObjectModelByAppId:(NSString *)appId{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    __block DNStoolModel *object = nil;
    
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@ where %@ = %@",Stool_TABLE,DairyBase_AppId,appId]];
        
        while ([rs next]) {
            
            object = [[DNStoolModel alloc] init];
            
            [object setAppId:[rs stringForColumn:DairyBase_AppId]];
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setInfantId:[rs stringForColumn:DairyBase_InfantId]];
            [object setStatus:[NSNumber numberWithInt:[[rs stringForColumn:DairyBase_Status] intValue]]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setDeleteTime:[rs stringForColumn:DairyBase_DeleteTime]];
            [object setDdat:[rs stringForColumn:DairyBase_Ddat]];
            [object setStoolYn:[rs stringForColumn:Stool_StoolYn]];
            [object setImage:[rs stringForColumn:Stool_Image]];
            [object setStoolCon:[NSNumber numberWithInt:[[rs stringForColumn:Stool_StoolCon] intValue]]];
            [object setAmount:[rs stringForColumn:Stool_amount]];
        }
        [db close];
    }];
    
    return object;

}

- (NSMutableArray *)getAllObjectModelNotSynchronized
{
    DNDataBase *dnDB = [DNDataBase shareInstance];
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString *synchronousDays = [[DNUserManager sharedInstance] getValueFromDefaultWithKey:UserDefaultKey_SynchronousDays];
        
        NSString * queryString =  [self getQueryAllModelSqlWithTableName:Stool_TABLE andBeforeDays:synchronousDays.intValue];
        FMResultSet *rs = [db executeQuery:queryString];
        while ([rs next]) {
            DNStoolModel *object = [[DNStoolModel alloc] init];
            
            [object setAppId:[rs stringForColumn:DairyBase_AppId]];
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setInfantId:[rs stringForColumn:DairyBase_InfantId]];
            [object setStatus:[NSNumber numberWithInt:[[rs stringForColumn:DairyBase_Status] intValue]]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setDeleteTime:[rs stringForColumn:DairyBase_DeleteTime]];
            [object setDdat:[rs stringForColumn:DairyBase_Ddat]];
            [object setStoolYn:[rs stringForColumn:Stool_StoolYn]];
            [object setImage:[rs stringForColumn:Stool_Image]];
            [object setStoolCon:[NSNumber numberWithInt:[[rs stringForColumn:Stool_StoolCon] intValue]]];
            [object setAmount:[rs stringForColumn:Stool_amount]];

            
            //添加对象到集合中
            [items addObject:object];
        }
        [db close];
    }];
    
    return items;
    
}

- (NSMutableArray *)getAppDisplayObjectModel
{
    
    DNDataBase *dnDB = [DNDataBase shareInstance];
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [dnDB.dbQueue inDatabase:^(FMDatabase *db) {
        
        if(![db open])
        {
            db = nil;
            return;
        }
        else
            [db setKey:DB_SECRETKEY];
        
        NSString * queryString =  [self getQueryAppDisplayModelSqlWithTableName:Stool_TABLE];
        FMResultSet *rs = [db executeQuery:queryString];
        while ([rs next]) {
            DNStoolModel *object = [[DNStoolModel alloc] init];
            
            [object setAppId:[rs stringForColumn:DairyBase_AppId]];
            [object setServerId:[rs stringForColumn:DairyBase_ServerId]];
            [object setInfantId:[rs stringForColumn:DairyBase_InfantId]];
            [object setStatus:[NSNumber numberWithInt:[[rs stringForColumn:DairyBase_Status] intValue]]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyReason]];
            [object setDeleteReason:[rs stringForColumn:DairyBase_DeleteReason]];
            [object setCreateTime:[rs stringForColumn:DairyBase_CreateTime]];
            [object setModifyTime:[rs stringForColumn:DairyBase_ModifyTime]];
            [object setDeleteTime:[rs stringForColumn:DairyBase_DeleteTime]];
            [object setDdat:[rs stringForColumn:DairyBase_Ddat]];
            [object setStoolYn:[rs stringForColumn:Stool_StoolYn]];
            [object setImage:[rs stringForColumn:Stool_Image]];
            [object setStoolCon:[NSNumber numberWithInt:[[rs stringForColumn:Stool_StoolCon] intValue]]];
            [object setAmount:[rs stringForColumn:Stool_amount]];
            
            //添加对象到集合中
            [items addObject:object];
        }
        [db close]; 
    }];
    
    return items;

}


@end
